package com.lxh.weekendmvc.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxh.weekendmvc.dao.EmpDao;
import com.lxh.weekendmvc.entity.Emp;
import com.lxh.weekendmvc.services.EmpServices;
import com.lxh.weekendmvc.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class EmpServicesImpl implements EmpServices {

    @Autowired
    private EmpDao empDao;

    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public Emp doLogin(String ename, String pwd) {
        System.out.println("按名字查询：");
        if(StringUtil.isNotEmpty(ename,pwd)){
            Emp emp = empDao.getEmpByName(ename);
            System.out.println(emp);
            if(emp!=null&&emp.getPwd().equals(pwd)){
                return emp;
            }
        }
        return null;

    }


    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public PageInfo<Emp> getEmpByPage(Map<String, String> emps) {
        System.out.println("分页查询");
        //获取当前的页数数值
        int num = StringUtil.stringToInt(emps.get("pageNum"));
        //创建pageHelper
        PageHelper.startPage(num, 5);
        List<Emp> all = empDao.getEmpByKey(emps);
        PageInfo<Emp> info = new PageInfo<>(all);
        return info;
    }

    @Override
    public boolean addEmp(Emp emp) {
        if(empDao.addEmp(emp)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean checkName(String ename) {
        if(StringUtil.isNotEmpty(ename)){
            if(empDao.getEmpByName(ename)!=null){
                return true;
            }
        }
        return false;
    }

    @Override
    public Emp getEmpByEno(Integer eno) {
        if(eno>0){
            return empDao.getEmpByEno(eno);
        }
        return null;
    }

    @Override
    public boolean updateEmp(Emp emp){
        if(empDao.updateEmp(emp)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delEmp(Integer eno) {
        if(empDao.delEmp(eno)>0){
            return  true;
        }
        return  false;
    }


}
